from typing import Annotated

from fastapi import Query
from pydantic import model_validator

from app.utils import types
from app.models.common import AppId, Pagination
from app.models import responses


class AppRechargeLogList(AppId, Pagination):
    """分页获取指定应用的充值日志记录"""
    origin_card: str
    used_card: str
    recharge_ip: str

    @model_validator(mode='before')
    @classmethod
    def before(cls, data):
        data = types.safe_dict(data)
        data['origin_card'] = data.get('origin_card', '')
        data['used_card'] = data.get('used_card', '')
        data['recharge_ip'] = data.get('recharge_ip', '')
        return data

    @model_validator(mode='after')
    def after(self):
        if len(self.origin_card) > 64:
            raise responses.FailureResponseError(-102, msg='充值的卡密长度不能超过 64 位')

        if len(self.used_card) > 64:
            raise responses.FailureResponseError(-102, msg='消耗的卡密长度不能超过 64 位')

        if len(self.recharge_ip) > 64:
            raise responses.FailureResponseError(-102, msg='充值地 IP 长度不能超过 64 位')

        return self


AppRechargeLogListParams = Annotated[AppRechargeLogList, Query()]
